package com.fire.social.service.impl;

import com.fire.social.entity.node.User;
import com.fire.social.mapper.FollowMapper;
import com.fire.social.mapper.RecommendMapper;
import com.fire.social.mapper.UserMapper;
import com.fire.social.service.RecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RecommendServiceImpl implements RecommendService {

    @Autowired
    private RecommendMapper recommendMapper;
    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private UserMapper userMapper;

    public boolean contains(List<User> arr, User target) {
        for (User user : arr) {
            if (user.getId().equals(target.getId()))
                return true;
        }
        return false;
    }

    @Override
    public void recommendFriend(User user, Map<String, Object> map) {
        List<User> users = recommendMapper.byFriend(user.getAccount());
        List<User> myfollow = userMapper.getMyFollow(user.getAccount());
        Set<User> res = new HashSet<>();
        for (User value : users) {
            if (value.getAccount().equals(user.getAccount()))
                continue;
            if (contains(myfollow, value)) continue;
            res.add(value);
        }
        Long myFollowNum = followMapper.countIFollow(user.getAccount());
        Long followMeNum = followMapper.countFollowMe(user.getAccount());
        map.put("myfollowing", myFollowNum);
        map.put("follower", followMeNum);
        map.put("recommends", res);
        map.put("user", user);
        map.put("index", "朋友推荐");
        map.put("title", "可能认识的人");
    }

    @Override
    public void recommendContent(User user, Map<String, Object> map) {
        List<User> users = recommendMapper.bycontent(user.getAccount());
        List<User> myfollowing = userMapper.getMyFollow(user.getAccount());
        HashSet<User> res = new HashSet<>();
        for (User value : users) {
            if (value.getAccount().equals(user.getAccount()))
                continue;
            if (contains(myfollowing, value)) continue;
            res.add(value);
        }
        Long myFollowNum = followMapper.countIFollow(user.getAccount());
        Long followMeNum = followMapper.countFollowMe(user.getAccount());
        map.put("myfollowing", myFollowNum);
        map.put("follower", followMeNum);
        map.put("recommends", res);
        map.put("user", user);
        map.put("index", "朋友推荐");
        map.put("title", "最常互动的人");
    }

    @Override
    public void recommendHobby(User user, Map<String, Object> map) {
        List<User> users = recommendMapper.byHobby(user.getAccount());
        List<User> myfollowing = userMapper.getMyFollow(user.getAccount());
        HashSet<User> res = new HashSet<>();
        for (User value : users) {
            if (value.getAccount().equals(user.getAccount()))
                continue;
            if (contains(myfollowing, value)) continue;
            res.add(value);
        }
        Long myFollowNum = followMapper.countIFollow(user.getAccount());
        Long followMeNum = followMapper.countFollowMe(user.getAccount());
        map.put("myfollowing", myFollowNum);
        map.put("follower", followMeNum);
        map.put("recommends", res);
        map.put("user", user);
        map.put("index", "朋友推荐");
        map.put("title", "趣味相投的人");
        map.put("reverse", false);
    }
}
